package com.miya.demo.config;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * SaToken配置
 *
 * @author Caixiaowei
 * @date 2022/07/08
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 下面定义了拦截器，会导致 spring.resources.static-locations 配置失效
        registry.addResourceHandler("/**").addResourceLocations("classpath:/webapp/public/");

        // 配置 knife4j 文档资源的访问路径
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 注册登录拦截器
        registry.addInterceptor(new SaRouteInterceptor())
//                .addPathPatterns("/**")
                .excludePathPatterns("/login/register", "/login", "/test/**",
                        "/swagger-resources/**", "/webjars/**", "/v2/**", "/v3/**", "/swagger-ui.html/**",
                        "/doc.html/**", "/error", "/favicon.ico", "/test/captcha/**", "/webSocket/**", "/websocket/**");

        // 注册注解拦截器
        registry.addInterceptor(new SaAnnotationInterceptor())
                .addPathPatterns("/**");
    }
}
